package com.originskyseed.platform.task.controller;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.task.i18n.TaskResponseMsg;
import com.originskyseed.platform.task.model.TaskRelateMaintainUser;
import com.originskyseed.platform.task.service.ITaskRelateMaintainUserService;
import com.originskyseed.platform.task.vo.request.TaskRelateMaintainUserRequestVO;

/**
 * 任务关联维护用户表Controller类（OSI_TASK_RELATE_MAINTAIN_USER）
 *
 * @author wlj
 */
@Controller
@RequestMapping("/taskRelateMaintainUser")
@BaseLocaleResource(localeFileName = "i18n_task")
public class TaskRelateMaintainUserController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(TaskRelateMaintainUserController.class);

	@DubboReference
	private ITaskRelateMaintainUserService taskRelateMaintainUserService;

	/**
	 * 分页查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/taskRelateMaintainUserList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<TaskRelateMaintainUser>> taskRelateMaintainUserList(HttpServletRequest request) {
		BaseResponseBody<BasePage<TaskRelateMaintainUser>> result = null;
		try {
			// 请求对象
			TaskRelateMaintainUserRequestVO requestVO = this.getParameterBean(TaskRelateMaintainUserRequestVO.class);

			// 分页查询
			BasePage<TaskRelateMaintainUser> page = new BasePage<TaskRelateMaintainUser>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.taskRelateMaintainUserService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<TaskRelateMaintainUser>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), TaskResponseMsg.TASK_RELATE_MAINTAIN_USER_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<TaskRelateMaintainUser>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), TaskResponseMsg.TASK_RELATE_MAINTAIN_USER_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/taskRelateMaintainUserSave.json", method = RequestMethod.POST)
	@RequiredParams(names = {})
	public BaseResponseBody<TaskRelateMaintainUser> taskRelateMaintainUserSave(HttpServletRequest request) {
		BaseResponseBody<TaskRelateMaintainUser> result = null;
		try {
			// 请求对象
			TaskRelateMaintainUser taskRelateMaintainUser = this.getParameterBean(TaskRelateMaintainUser.class);

			if (StringUtils.isNotBlank(taskRelateMaintainUser.getId())) {
				this.taskRelateMaintainUserService.updateByPrimaryKey(taskRelateMaintainUser);
			} else {
				this.taskRelateMaintainUserService.insertSelective(taskRelateMaintainUser);
			}
			result = new BaseResponseBody<TaskRelateMaintainUser>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), TaskResponseMsg.TASK_RELATE_MAINTAIN_USER_SAVE_SUCCESS.getMsg()), taskRelateMaintainUser);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<TaskRelateMaintainUser>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), TaskResponseMsg.TASK_RELATE_MAINTAIN_USER_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/taskRelateMaintainUserDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> taskRelateMaintainUserDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});

			this.taskRelateMaintainUserService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), TaskResponseMsg.TASK_RELATE_MAINTAIN_USER_DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), TaskResponseMsg.TASK_RELATE_MAINTAIN_USER_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

}
